package com.skytech.chatim.sky.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.util.Log;

public class DataUtil {
	private static String TAG = DataUtil.class.getSimpleName();
    public static final String SKY_USERNAME = "SKY_USERNAME";// 账户
    public static final String SKY_PASSWORD = "SKY_PASSWORD";// 密码
    private static final String PREFERENCES = "PREFERENCES" ;
    public static final String SKY_TOKEN = "TOKEN" ;
    public static final String MySKyUser = "MySKyUser" ;
    public static final String IntentKey = "IntentKey" ;
    public static final String HasRun = "HasRun" ;
    public static final String DataDir = "skytech" ;
    public static final String Pass1 = "P@ss1234" ;
    public static final String Pass2 = "Pass1234" ;
    public static final String Pass3 = "skytech" ;
    public static final String Pass4 = "czqmail" ;   
    public static String user  ;
    
    public static String readGlobalPreferences(Context context, final String key) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES,
                Context.MODE_PRIVATE);
        return preferences.getString(key, "");
    }
    

    public static void writeToGlobalPreferences(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES,0);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    public static String readFromPreferences(Context context, final String key) {
        SharedPreferences preferences = context.getSharedPreferences(getUser(context),
                Context.MODE_PRIVATE);
        return preferences.getString(key, "");
    }
    


    private static String getUser(Context context) {
        if (isEmpty(user)){
            user = readGlobalPreferences(context,SKY_USERNAME);
        }
        Log.d(TAG, " getUser " + user);
        return user;
    }


    public static void writeToPreferences(Context context, String key,String value) {
        SharedPreferences preferences = context.getSharedPreferences(getUser(context),0);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    


    
	/**
	 * 对象序列化(该对象要实现Serializable接口)，保存到内存(ROM)
	 */
	public static boolean writeObject(Context context, String file, Serializable object) {
		FileOutputStream out = null;
		ObjectOutputStream oos = null;
		try {
			out = context.openFileOutput(file, Context.MODE_PRIVATE);
			oos = new ObjectOutputStream(out);
	        oos.writeObject(object);
	        out.close();
	        oos.close();
	        return true;
		} catch (Exception e) {
			e.printStackTrace(); 
			return false;
		} finally {//  关闭流
			try {
				if (oos != null) {
					oos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 读取内存中序列化的对象
	 * @param context
	 * @param key
	 * @return
	 */
	public static Object readObject(Context context,  String key) {
		FileInputStream in = null;
		ObjectInputStream inputStream = null;
        try {
            in = context.openFileInput(key);
            if (in != null) {
                inputStream = new ObjectInputStream(in);
                return inputStream.readObject();
			}
            return null;
        } catch (Exception e) {
        	Log.e(TAG, "file "+key+" not find");
            return null;
        }finally{
    		try {
    			if (in != null) {
    				in.close();
    			}
    			if (inputStream != null) {
    				inputStream.close();
    			}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
	}

    public static boolean isEmpty(String string) {
        return string == null || string.equals("");
    }


    public static void setDir() {
         File dir = Environment.getExternalStoragePublicDirectory(DataDir);
         if (!dir.exists()){
             dir.mkdir();
         }
    }
}
